<?php


class KnowledgeCategoryModel extends BaseModel
{
    protected $table = 'yf_knowledge_category';

    protected $principal = 'id';

    const status = [
        'open' => 1,//开启
        'close' => -1,//关闭
    ];

    /**
     * 获取全部正常使用的分类
     * @return array|bool
     */
    public function findAllOpen()
    {
        $list = $this->findAll(['status' => self::status['open'], 'ORDER' => 'level'], [
            'id', 'name', 'parent_id', 'image', 'level'
        ]);
        return $this->getSubs($list, 0);
    }

    /**
     * 递归分类
     * @param $categorys
     * @param $parent_id
     * @return array
     */
    private function getSubs($categorys, $parent_id){
        $subs = [];
        foreach ($categorys as $category) {
            if ($category['parent_id'] == $parent_id) {
                $category['sub'] = $this->getSubs($categorys, $category['id']);
                if (!$category['sub']) {
                    unset($category['sub']);
                }
                $subs[] = $category;
            }
        }
        return $subs;
    }

}